class Animation {
    constructor(dir) {
        this.dir = dir
        this.type = 'Animation'
        
        this.walkframes = [
            'images/walk/walk1.png',
            'images/walk/walk2.png',
            'images/walk/walk3.png',
            'images/walk/walk4.png',
            'images/walk/walk5.png',
            'images/walk/walk6.png',
            'images/walk/walk7.png',
            'images/walk/walk8.png',
            'images/walk/walk9.png',
            'images/walk/walk10.png',
        ]
        this.idleframes =[
            'images/idle/idle1.png',
            'images/idle/idle2.png',
            'images/idle/idle3.png',
            'images/idle/idle4.png',
            'images/idle/idle5.png',
            'images/idle/idle6.png',
            'images/idle/idle7.png',
            'images/idle/idle8.png',
            'images/idle/idle9.png',
            'images/idle/idle10.png',
        ]
        this.map={
            'idle':this.idleframes,
            'walk':this.walkframes,
        }
        this.id = window.Aid
        this.count()
        this.animationIndex = 1;
        this.max_speed = 4
        this.speed = this.max_speed
        this.x = 0
        this.y = -100
        this.player = new Sprite(this.dir, this.texture, this.x, this.y)
        this.currState='idle'
        this.frames=[]
        this.frames=this.map[this.currState]
        this.texture = this.frames[0]
    }
    count() {
        window.Sid++
    }
    static reduce() {
        window.Sid--
    }
    setSpeed(x) {
        if (x < 0) {
            this.max_speed = 0
            return
        }
        this.max_speed = x

    }
    changeAnimation(type){
        this.currState=type
        this.frames=this.map[type]
    }
    move(x,state) {
        this.player.x += x
        if(x<0){
            this.filpx=true
        }else{
            this.filpx=false
        }
        if(state=='down'){
            this.changeAnimation('walk')
        }else if(state=='up'){
            this.changeAnimation('idle')
        }

    }
    update() { 
        if (this.speed > 0) {
            this.speed--
            return
        } else {
            this.speed = this.max_speed
        }
        this.animationIndex = (this.animationIndex + 1) % this.frames.length + 1
        var index = this.animationIndex
        this.texture = this.frames[index]
    }
    draw() {
        this.player.imageFrom(this.texture)
        if (this.filpx) {
            this.player.flipx()
        } else {
            this.dir.drawNode(this.player)
        }
    }
}